var contacts_sent;
var contacts_dirty = true;
var contacts_content;
var contacts_tab;
var contacts_bNew;
var contacts_bUpdate;
var contacts_accounts;
var contacts_newGroup;
var contacts_NEWPOPUP="contacts_newPopup";

function contacts_init() {
	contacts_content = document.getElementById("contacts_content");
	contacts_tab = main_createTab("contacts_content", _T("Contacts"));
	main_contents.push(contacts_content);
	contacts_bNew = main_createTool(_T("NewContact"), "contacts_newContact();");
	contacts_bUpdate = main_createTool(_T("Update"), "contacts_dirty=true;contacts_read();");
}

function contacts_show() {
	main_toolbar.appendChild(contacts_bNew);
	main_toolbar.appendChild(contacts_bUpdate);
	contacts_read();
}

function contacts_read(){
	if (contacts_dirty) {
		contacts_dirty = false;
		contacts_content.innerHTML = _T("WaitDots");
		var req = new XMLHttpRequest();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {
					addPing(new Date().getTime() - contacts_sent);
					contacts = main_readJSONResponse(req);
					if(!contacts){
						return;
					}
					contacts_accounts = contacts.accounts;
					contacts_writeContacts(contacts.contacts);
					window.scrollTo(0,main_scrollTop);
				}
			}
		};
		main_encryptReq(req,"contacts");
		contacts_sent = new Date().getTime();
	}
}

function contacts_writeContacts(_contacts) {
	while (contacts_content.firstChild) {
		contacts_content.removeChild(contacts_content.firstChild);
	}
	var table = document.createElement("table")
	for ( var i = 0; i < _contacts.length; i++) {
		var contact = _contacts[i];
		var count = 0;
		var tdContact = document.createElement("td");
		//var theName = "";
		//theName += _contacts.name;
		tdContact.textContent = i+1;
		for ( var rawContactId in contact.raws) {
			count++;
			var rawContact = contact.raws[rawContactId];
			var trContact = document.createElement("tr");
			if (count == 1) {
				trContact.appendChild(tdContact);
			}
			// Name
			var tdName = document.createElement("td");
			var name = "";
			if (rawContact.nick != null && rawContact.nick != "") {
				name = "'" + rawContact.nick + "' ";
			}
			name += rawContact.name;
			tdName.innerHTML = "<a class=\"file\" href=\"#\" onclick=\"contacts_showDetails('"
					+ rawContactId + "'); return false;\">"+name+"</a>";
			trContact.appendChild(tdName);
			// Emails
			var tdEmails = document.createElement("td");
			contacts_arrayToDivs(tdEmails, rawContact.emails);
			trContact.appendChild(tdEmails);
			// Phones
			var tdPhones = document.createElement("td");
			contacts_arrayToDivs(tdPhones, rawContact.phones);
			trContact.appendChild(tdPhones);
			// alert(debugObject(trContact));
			table.appendChild(trContact);
		}
		tdContact.setAttribute("rowspan", count);
	}
	contacts_content.appendChild(table);
}

function contacts_arrayToDivs(_parent, _array) {
	if (_array) {
		for ( var i = 0; i < _array.length; i++) {
			var div = document.createElement("div");
			div.appendChild(document.createTextNode(_array[i]));
			_parent.appendChild(div);
		}
	}
}

function contacts_newContact() {
	var content = document.createElement("div");
	var textDiv=document.createElement("div");
	textDiv.textContent=_T("NewContactSelectGroup");
	content.appendChild(textDiv);
	contacts_newGroup = document.createElement("select");
	contacts_newGroup.multiple = false;
	var preffered = 0;
	for ( var i = 0; i < contacts_accounts.length; i++) {
		var acc = contacts_accounts[i];
		if ((acc.type = "com.google") && (acc.name.indexOf("@")>0)) {
			preffered = i;
			var option = document.createElement("option");
			option.textContent = acc.name + " / " + acc.type;
			option.selected = true;
			contacts_newGroup.add(option);
			break;
		}
	}
	for ( var i = 0; i < contacts_accounts.length; i++) {
		if (i != preffered) {
			var acc = contacts_accounts[i];
			var option = document.createElement("option");
			option.textContent = acc.name + " / " + acc.type;
			contacts_newGroup.add(option);
		}
	}
	var selectDiv=document.createElement("div");
	selectDiv.appendChild(contacts_newGroup);
	content.appendChild(selectDiv);
	// Toolbar
	var toolbar = document.createElement("div");
	toolbar.style.textAlign = "right";
	toolbar.appendChild(main_createButton(_T("Create"),
			"contacts_newContactCreate()"));
	toolbar.appendChild(main_createButton(_T("Cancel"),
	"popup_hide('"+contacts_NEWPOPUP+"')"));
	content.appendChild(toolbar);
	var popup=popup_show(_T("NewContact"),content,contacts_NEWPOPUP);
	popup.frame.style.width=(selectDiv.offsetWidth+20)+"px";
}

function contacts_newContactCreate(){
	main_scrollTop=document.documentElement.scrollTop;
	var req = new XMLHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				addPing(new Date().getTime() - contactsReqSent);
				response = main_readJSONResponse(req);
				if(!response){
					return;
				}
				popup_hide(contacts_NEWPOPUP);
				contacts_showDetails(response.rawId);
				contacts_dirty = true;
				contacts_read();
			}
		}
	};
	var acc=contacts_newGroup.options[contacts_newGroup.selectedIndex].textContent.split(" / ");
	var spec=new Object();
	spec.name=acc[0];
	spec.type=acc[1];
	main_encryptReq(req,"contactcreate",spec);
	contactsReqSent = new Date().getTime();
}